两种搜索方式,对 unordered array 用 linear search,对 ordered array 用 binary search。
二分查找 (Binary search)
概念
对于已排序的有序线性容器而言(比如数组,vector),二分查找(Binary search)几乎总是最优的搜索方案。二分查找将容器等分为两部分,再根据中间节点与待搜索数据的相对大小关系,进一步搜索其中某一部分。二分查找的算法复杂度为O(logn)。
对于局部有序的数据,也可以根据其局部有序的特性,尽可能地利用逼近、剪枝,使用二分查找的变种进行搜索。
二分寻找要注意的问题是:
- Which way should middle pointer go next
- Avoid infinite loop in the code
算法
Compare the number in the middle of the array with x. If it is equal, we are done. If the number is greater, we know to look in the second half of the array. If it is smaller, we know to look in the first half. We can repeat the search on the appropriate half of the array by comparing the middle element of that array with x, once again narrowing our search by a factor of 2. We repeat this process until we find x. This algorithm takes O(log n) time.
Complexity
Time complexity: O(logN)
Worst case: O(logN+1) -> O(logN)
模板
Recursive
Non-recursive
Find closest value
例题
69. Sqrt(x)
Problem
Implement int sqrt(int x).
Compute and return the square root of x.
Solution
|
|
Find first bad version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
|
|
Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
把这个 2D 数组拉平成 1D 就是一个单调递增的(sorted)的数组,这种情况下找一个数用 binary search 就好,时间复杂度是 O(log(mn))=O(logN),要注意的就是 index 之间怎么转换,观察发现:
2D -> 1D (i,j) -> i*n+j
1D -> 2D index -> (index/n,index%n)
Search a 2D Matrix II
问题再变难一点。
Search a 2D Matrix II QuestionEditorial Solution My Submissions
Total Accepted: 50080
Total Submissions: 136871
Difficulty: Medium
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
For example,
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false.
当然还是可以用 binary search 做,需要非常仔细。时间复杂度 max(O(mlogn,nlogm)),由 T(n)=2T(n/2)+cn 推导而来
或者,不用 binary search,用比较通用的方法,很好理解,从 top rightmost 开始,比较与 target 的大小,if curr>target,往左,if curr
475. Heaters
Problem
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.
Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters.
So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters.
Note:
Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
As long as a house is in the heaters’ warm radius range, it can be warmed.
All the heaters follow your radius standard and the warm radius will the same.
Example 1:
Example 2:
Solution
|
|